import re

from flask import Flask, render_template, jsonify,request
import requests

app = Flask(__name__)


@app.route("/")
def index():
    return render_template("tt.html")

@app.route("/get")
def get():
    code = request.args.get('data')
    return jsonify({'data':get_info(code),'success':'0'})

def get_info(code):
    try:
        header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
        res = requests.get('https://fund.eastmoney.com/js/fundcode_search.js')
        data = res.text
        ff = re.findall('var r = (.*?);', data)
        info = eval(ff[0])
        title = ''
        for i in info:
            if code == i[0]:
                title = i[2]

        url = f'https://danjuanapp.com/djapi/fund/growth/{code}?day=30'
        res = requests.get(url, headers=header)
        data  = res.json()
        if data.get('data'):
            x1 = data.get('data')['fund_nav_growth']
            time1 = []
            sy = []
            for d in x1:
                t = d.get('date')
                date = t.replace(t.split('-')[0]+'-','')
                z = d.get('percentage')
                time1.append(date)
                sy.append(z)
            return {'time':time1,'price':sy,'title':title}
    except Exception as e:
        print(e)
    return None

if __name__ == '__main__':
    app.run()
